package com.easy.auth.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;import java.util.*; 

import javax.servlet.http.*;

import com.easy.auth.domain.UploadFile;
import com.easy.auth.service.UploadService;
import com.easy.common.FileBaseAction;
import com.easy.common.jdbc.PageInfo;
import com.easy.common.util.DateUtil;
import com.easy.common.web.FilterUtil;
import com.easy.common.web.PagedList;
import com.easy.common.web.message.DeleteMessage;
import com.easy.common.web.message.Message;
import com.easy.common.web.message.SaveOrEditMessage;

@Controller
@RequestMapping("UpLoadController")
public class UpLoadController extends FileBaseAction{
	
	@Autowired
	private UploadService uploadService;
	
 

		
		@RequestMapping("goToUpLoadManagerPage")
		public String goToUpLoadManagerPage(String secondMenuId,
				HttpServletRequest request) {
			request.getSession().setAttribute("secondMenuId", secondMenuId);
			return "system/icon";
		}
		
		@RequestMapping("getUploadFiles")
		@ResponseBody
		public PagedList<UploadFile> getUploadFiles(PageInfo pageInfo) {
			UploadFile uploadFile = (UploadFile) FilterUtil.getVO(UploadFile.class,
					pageInfo.getFilterRules());
			 pageInfo.setSort("orders");
			 pageInfo.setOrder("ASC");
			return uploadService.getAllUploadFilesLike(uploadFile, pageInfo);
		}

		@RequestMapping("getUploadFileById")
		@ResponseBody
		public UploadFile getUploadFileById(UploadFile uploadFile) {
			return uploadService.findUploadFileByOid(uploadFile);
		}
		@RequestMapping("saveUploadFile")
		@ResponseBody
		public Message saveUploadFile(HttpSession httpSession, UploadFile uploadFile,MultipartFile file) throws IOException {
			
			Resource resource = new ClassPathResource("/jdbc.properties");
			Properties props = PropertiesLoaderUtils.loadProperties(resource);
			String ctxPath = props.getProperty("upload.file.path");
			
			File dirPath = new File(ctxPath);
			if (!dirPath.exists()) {
				dirPath.mkdirs();
			}
			String orders = uploadFile.getOrders();
			if(orders.length()<3){
				uploadFile.setOrders(("000"+orders).substring(orders.length(),orders.length()+3));
			}
			String suffix = file.getOriginalFilename().substring(
					file.getOriginalFilename().lastIndexOf(".") + 1,
					file.getOriginalFilename().length());
			
			// 实际存放文件名称
			String fileStoreName = DateUtil.getSystemDate("yyyyMMddHHmmssS") + "."
					+ suffix;
			if(suffix.equals("")){
				return new SaveOrEditMessage(uploadService.saveUploadFile(uploadFile));
			}
			File uploadImg = new File(ctxPath + "/" + fileStoreName);
			FileCopyUtils.copy(file.getBytes(), uploadImg);
			
			
			uploadFile.setLocalname(fileStoreName);
			uploadFile.setFilename(file.getOriginalFilename());
			uploadFile.setSize(String.valueOf(file.getSize()));
			uploadFile.setPath(ctxPath + fileStoreName);
			
			return new SaveOrEditMessage(uploadService.saveUploadFile(uploadFile));
		}
		
		@RequestMapping("removeUploadFile")
		@ResponseBody
		public Message removeUploadFile(String oids) {
			String[] _oids = oids.split("-");
			List<UploadFile> iconlist = new ArrayList<UploadFile>();
			for (int i = 0; i < _oids.length; i++) {
				UploadFile uploadFile = new UploadFile();
				uploadFile.setOid(_oids[i]);
				iconlist.add(uploadFile);
			}
			return new DeleteMessage(uploadService.removeUploadFile(iconlist));
		}
		@RequestMapping("getAllUploadFile")
		@ResponseBody
		public List<UploadFile> getAllUploadFile() {
			return uploadService.findUploadFile();
		}
	 

	}

 








